package com.jsj.servlet.gb;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsj.dao.IUserDao;
import com.jsj.dao.impl.UserDaoImpl;
import com.jsj.model.User;
import com.jsj.utils.StringUtils;

/**
 * 负责完成用户信息的CRUD、登录、注销等功能
 * @author Administrator
 *
 */

@WebServlet("/user")
public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = -3704200856467333570L;
	
	private IUserDao userDao=new UserDaoImpl();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");		
		PrintWriter out=response.getWriter();
		
		String type=request.getParameter("type");
		
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		Boolean flag=false;
		
		int ret=0;
		
		if(type.equals("login")) {
			login(request, response, out, username, password);
		}
		if(type.equals("reg")) {
			reg(request, response, username, password);
		}
	}

	private void reg(HttpServletRequest request, HttpServletResponse response, String username, String password)
			throws ServletException, IOException {
		int ret;
		if(StringUtils.isNotNullAndEmpty(username) && StringUtils.isNotNullAndEmpty(password)) {				
			if(userDao.isExist(username)) {
				//重名
				request.setAttribute("regInfo", "用户名已经使用，请更换！");
				RequestDispatcher dispatcher=request.getRequestDispatcher("/reg.jsp");
				dispatcher.forward(request, response);
			}else {
				//不重名
				User u=new User();
				u.setUsername(username);
				u.setPassword(password);
				u.setRoleId(2);
				ret=userDao.add(u);
				if(ret!=0) {					
					RequestDispatcher dispatcher=request.getRequestDispatcher("/login.jsp");
					dispatcher.forward(request, response);
				}else {
					request.setAttribute("regInfo", "注册失败！");
					RequestDispatcher dispatcher=request.getRequestDispatcher("/reg.jsp");
					dispatcher.forward(request, response);
				}
			}				
			
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response, PrintWriter out, String username,
			String password) throws ServletException, IOException {
		Boolean flag;
		if(StringUtils.isNotNullAndEmpty(username) && StringUtils.isNotNullAndEmpty(password)) {//null
			flag=userDao.login(username, password);
			if(flag) {
				request.setAttribute("loginInfo", username);
				RequestDispatcher dispatcher=request.getRequestDispatcher("/index.jsp");
				dispatcher.forward(request, response);
			}else {
				request.setAttribute("loginInfo", "用户名或密码错误！");
				RequestDispatcher dispatcher=request.getRequestDispatcher("/login.jsp");
				dispatcher.forward(request, response);
			}
		}else {
			out.println("用户名或者密码不能为空！");
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(requset, response);
	}
	

}
